﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace asp.netWebFroms
{
    public partial class _Default : Page
    {
        private static DataTable data = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            var sql = "select * from Articles";
            data = DbHelper.GetData(sql);
            GridView1.DataSource = data;
            GridView1.DataBind();
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            FillData();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            FillData();
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var idStr = GetCellVaule(e.RowIndex, 0);
            var isOk = int.TryParse(idStr, out int id);
            var title = GetCellVaule(e.RowIndex, 1);
            var content = GetCellVaule(e.RowIndex, 2);
            var author = GetCellVaule(e.RowIndex, 3);
            var remarks = GetCellVaule(e.RowIndex, 4);
            if (isOk)
            {
                var sql = String.Format("update Articles set Title='{0}',Content='{1}',Author='{2}',Remarks='{3}'where Id='{4}'", title, content, author, remarks, id);
                DbHelper.AddOrDeleteOrUpdate(sql);
            }
            else
            {
                var sql = String.Format("insert into Articles (Title,Content,Author,Remarks) values ('{0}','{1}','{2}','{3}')", title, content, author, remarks);
                DbHelper.AddOrDeleteOrUpdate(sql);
            }
           
            GridView1.EditIndex = -1;
            FillData();
        }

        private string GetCellVaule(int rowIndex, int v)
        {
            var control = GridView1.Rows[rowIndex].Cells[v];
            var dt = control.Controls.Count>0?((TextBox)control.Controls[0]).Text:control.Text;
            return dt;
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GetCellVaule(e.RowIndex, 0);
            var sql = string.Format("delete from Articles where Id='{0}'", id);
            DbHelper.AddOrDeleteOrUpdate(sql);
            GridView1.EditIndex = -1;
            FillData();

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            var dt= data.Copy();
            dt.Rows.Add(dt.NewRow());
            GridView1.DataSource = dt;
            GridView1.DataBind();
            GridView1.EditIndex = dt.Rows.Count - 1;
            GridView1.DataSource = dt;
            GridView1.DataBind();

        }
    }
}